package org.shock.ss.sms.service.system.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.alibaba.fastjson.JSON;
import org.shock.ss.sms.commons.db.ProxoolDbPool;
import org.shock.ss.sms.commons.db.TsDbTools;
import org.shock.ss.sms.commons.utils.ReflexUtils;
import org.shock.ss.sms.bean.system.Module;
import org.shock.ss.sms.bean.system.Portal;
import org.shock.ss.sms.bean.system.Right;
import org.shock.ss.sms.bean.system.Role;
import org.shock.ss.sms.bean.system.User;
import org.shock.ss.sms.bean.system.UserInfor;
import org.shock.ss.sms.commons.dao.BaseDao;
import org.shock.ss.sms.commons.dao.DaoFactory;
import org.shock.ss.sms.dao.system.UserDao;
import org.shock.ss.sms.commons.service.impl.BaseServiceImpl;
import org.shock.ss.sms.service.system.UserService;

public class UserServiceImpl extends BaseServiceImpl implements UserService {
	private UserDao dao = (UserDao) DaoFactory.getDao("UserDao");

	public UserServiceImpl(BaseDao dao) {
		super(dao);
	}

	public UserInfor checkUser(String code, String pwd) throws Exception {
		String ygzh = dao.checkUser(code, pwd);
		UserInfor userinfor = null;
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			if (null != ygzh) {
				// 登录成功后 封装userinfor信息
				String sql = "select y.*,d.bmmc,(select group_concat(modids) from ckgl_role where ','||y.roleids||',' like '%,'||id||',%') as modids,(select group_concat(rightids) from ckgl_role where ','||y.roleids||',' like '%,'||id||',%') as rightids from "
						+ User.getTable() + " y left join ckgl_dept d on y.bmid=d.id  where y.ygzh='" + ygzh + "'";
				System.out.println(sql);
				Map map = dao.view(sql);

				userinfor = new UserInfor();
				BeanUtils.populate(userinfor, map);
				// 部门名称
				userinfor.setBmmc((String) map.get("bmmc"));
				// 用户模块ids
				userinfor.setModids((String) map.get("modids"));
				// 用户权限ids
				userinfor.setRightids((String) map.get("rightids"));
				// 用户权限代码
				String sql2 = "select code from " + Right.getTable() + " where id in (0" + map.get("rightids") + ")";
				System.out.println(sql2);
				List rlist = TsDbTools.getStringList(sql2);
				userinfor.setRightcodes(rlist);
				// 用户角色代码
				String sqlrightcodes = "select code from " + Role.getTable() + " where id in ("
						+ userinfor.getRoleids() + ")";
				List rolist = TsDbTools.getStringList(sqlrightcodes);
				userinfor.setRolecodes(rolist);
				// 用户可拥有的桌面protal
				String sql3 = "select * from " + Portal.getTable() + " where userid=" + userinfor.getId();
				List plist = TsDbTools.getMapsInList(sql3);
				userinfor.setPortals(JSON.toJSONString(plist));
			}
			tx.commitAndClose();
		} catch (Exception e) {
			tx.rollbackAndClose();
			throw e;
		}
		return userinfor;
	}

	public UserInfor checkUserPki(String sfz) throws Exception {
		String ygzh = dao.checkUserPki(sfz);
		UserInfor userinfor = null;
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			if (null != ygzh) {
				// 登录成功后 封装userinfor信息
				String sql = "select y.*,d.bmmc,(select group_concat(modids) from ckgl_role where ','||y.roleids||',' like '%,'||id||',%') as modids,(select group_concat(rightids) from ckgl_role where ','||y.roleids||',' like '%,'||id||',%') as rightids from "
						+ User.getTable() + " y left join dept d on y.bmid=d.id  where y.ygzh='" + ygzh + "'";
				Map map = dao.view(sql);
				userinfor = new UserInfor();
				BeanUtils.populate(userinfor, map);
				// 部门名称
				userinfor.setBmmc((String) map.get("bmmc"));
				// 用户模块ids
				userinfor.setModids((String) map.get("modids"));
				// 用户权限ids
				userinfor.setRightids((String) map.get("rightids"));
				// 用户权限代码
				String sql2 = "select code from right where id in (" + map.get("rightids") + ")";
				List rlist = TsDbTools.getStringList(sql2);
				userinfor.setRightcodes(rlist);
				// 用户角色代码
				String sqlrightcodes = "select code from ckgl_role where id in (" + userinfor.getRoleids() + ")";
				List rolist = TsDbTools.getStringList(sqlrightcodes);
				userinfor.setRolecodes(rolist);
				// 用户可拥有的桌面protal
				String sql3 = "select * from portal where userid=" + userinfor.getId();
				List plist = TsDbTools.getMapsInList(sql3);
				userinfor.setPortals(JSON.toJSONString(plist));
			}
			tx.commitAndClose();
		} catch (Exception e) {
			tx.rollbackAndClose();
			throw e;
		}
		return userinfor;
	}
}
